const express = require('express');
const multer = require('multer');
const fs = require('fs');
const path = require('path');
const cors=require('cors');
// 创建 Express 实例
const app = express();
app.use(cors())
// 配置 Multer 存储
const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    const uploadDir = path.join(__dirname, 'uploads');
    if (!fs.existsSync(uploadDir)) {
      fs.mkdirSync(uploadDir);
    }
    cb(null, uploadDir); // 设置文件保存路径
  },
  filename: (req, file, cb) => {
    const ext = path.extname(file.originalname); // 获取文件扩展名
    cb(null, Date.now() + ext); // 生成唯一文件名
  }
});

// 创建 multer 实例
const upload = multer({
    storage,
    limits: { fileSize: 10 * 1024 * 1024 }, // 限制每个文件最大 10MB
  });
  

// 处理文件上传的路由
app.post('/upload', upload.array('uploadFiles'), (req, res) => {
  if (!req.files || req.files.length === 0) {
    return res.status(400).send('No files were uploaded.');
  }

  // 打印上传的文件信息
  console.log('Uploaded files:', req.files);

  res.status(200).send({ message: 'Files uploaded successfully!' });
});

// 启动服务器
const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});
